package com.toe.forum.webapi.redisRepository.impl;

import com.toe.forum.webapi.redisRepository.IArticleRedisRepository;
import com.toe.pojo.vo.forum.ArticleDetailVO;
import com.toe.pojo.vo.forum.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Repository
public class ArticleRedisRepositoryImpl implements IArticleRedisRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    /**
     * 用于点赞量,收藏量的存储,方便增减
     */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    Random random = new Random();

    @Override
    public void saveEmptyValue(Long id) {
        String key = KEY_ARTICLE_ITEM_PREFIX+id;
        redisTemplate.opsForValue().set(key,null,10, TimeUnit.SECONDS);
    }

    @Override
    public boolean exists(Long id) {
        String key = KEY_ARTICLE_ITEM_PREFIX+id;
        return redisTemplate.hasKey(key);
    }

    @Override
    public void save(ArticleVO articleVO) {
        String key = KEY_ARTICLE_ITEM_PREFIX+articleVO.getId();
        redisTemplate.opsForValue().set(key,articleVO,60*60*4+ random.nextInt(60*30), TimeUnit.SECONDS);
    }

    @Override
    public ArticleVO selectArticleById(long id) {
        String key = KEY_ARTICLE_ITEM_PREFIX+id;
        Serializable result = redisTemplate.opsForValue().get(key);
        if(result == null){
            return null;
        }
        ArticleVO articleVO  = (ArticleVO) result;
        return articleVO;
    }

    @Override
    public void deleteArticleById(long id) {
        String key = KEY_ARTICLE_ITEM_PREFIX+id;
        redisTemplate.delete(key);
    }

    @Override
    public void saveArticleLike(long articleId, long count) {
        String key = KEY_ARTICLE_Like_PREFIX+articleId;
        stringRedisTemplate.boundValueOps(key)
                .set(count+"",60*60*4+random.nextInt(60*30),
                        TimeUnit.SECONDS);
    }

    @Override
    public long selectArticleLikeById(Long id) {
        String key = KEY_ARTICLE_Like_PREFIX+id;
        String s = stringRedisTemplate.opsForValue().get(key);
        return Long.parseLong(s);
    }

    @Override
    public void deleteArticleLikeById(Long id) {
        String key = KEY_ARTICLE_Like_PREFIX +id;
        redisTemplate.delete(key);
    }

    @Override
    public void saveArticleEnshrine(long articleId, long count) {
        String key = KEY_ARTICLE_Enshrine_PREFIX+articleId;
        stringRedisTemplate.boundValueOps(key)
                .set(count+"",60*60*4+random.nextInt(60*30),
                        TimeUnit.SECONDS);
    }

    @Override
    public long selectArticleEnshrineById(Long id) {
        String key = KEY_ARTICLE_Enshrine_PREFIX+id;
        String s = stringRedisTemplate.opsForValue().get(key);
        return Long.parseLong(s);
    }

    @Override
    public void deleteArticleEnshrineById(Long id) {
        String key = KEY_ARTICLE_Enshrine_PREFIX+id;
        redisTemplate.delete(key);
    }

    @Override
    public void savaArticleLook(long articleId,long count) {
        String key = KEY_ARTICLE_Look_PREFIX+articleId;
        stringRedisTemplate.boundValueOps(key)
                .set(count+"",60*60*4+random.nextInt(60*30),
                        TimeUnit.SECONDS);
    }

    @Override
    public long selectArticleLookById(Long id) {
        String key = KEY_ARTICLE_Look_PREFIX+id;
        String s = stringRedisTemplate.opsForValue().get(key);
        return Long.parseLong(s);
    }

    @Override
    public void deleteArticleLookById(Long id) {
        String key = KEY_ARTICLE_Look_PREFIX+id;
        redisTemplate.delete(key);
    }


    @Override
    public void deleteAllItem() {
        //使用通配符,把所有的用户缓存选中
        Set<String> keys = redisTemplate.keys("article:"+"*");
        redisTemplate.delete(keys);
    }
}
